﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Drawing2D;

namespace Grizzy_X
{
    //仅对一个多边形有岛
    public class CPolygon
    {
        private int ID;
        public List<List<PointF>> Polygons;
        public List<int> PolyCounts;
        public MBR MinBR;
        public int LegendID;
        //public int IslandNum;
        //public List<int> IslandIDs;
        public List<PointF> island;
        public bool isselected;
        public Pen polygonpen;
        public SolidBrush polygonbrush;
        public PointF inter1;
        public PointF inter2;
        //分别记录每个简单实体的数据，最后一个数据为整个之和
        //之后需要更改，因为未考虑岛的情况
        public double[] Perimeter;
        public double[] Area;
        public int GetID()
        {
            return ID;
        }

        public void newCpolygon(int pnid,int legdid)
        {
            ID = pnid;
            LegendID = legdid;
            Polygons = new List<List<PointF>>();
            PolyCounts = new List<int>();
            MinBR = new MBR();
            //IslandIDs = new List<int>();
            isselected = false;
        }

        public void addnode(PointF currentpoint)
        {
            Polygons[Polygons.Count() - 1].Add(currentpoint);

        }
        public void updateMBR()
        {
            List<PointF> polygonpoints = new List<PointF>();
            for (int i = 0; i < Polygons.Count(); i++)
            {
                for (int j = 0; j < Polygons[i].Count(); j++)
                    polygonpoints.Add(Polygons[i][j]);
            }
            if (polygonpoints.Count() > 0)
            {
                MinBR.GetMBR(polygonpoints);
            }
        }
        public void deletenode(int index1, int index2)
        {
            Polygons[index1].RemoveAt(index2);
        }
        public void deletesimplepolygon()
        {
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                Polygons.RemoveAt(Controller.selectsimple[i]);
                Polygons.Insert(Controller.selectsimple[i], new List<PointF>());
            }
            for (int i = 0; i < Polygons.Count(); i++)
            {
                if (Polygons[i].Count() == 0)
                {
                    Polygons.RemoveAt(i);
                    i--;
                }
            }
        }
        public void DragPolygon(Point movepoint)
        {
            for (int i = 0; i <Polygons.Count(); i++)
            {
                for (int j = 0; j < Polygons[i].Count(); j++)
                {
                    Point screen = new Point();
                    screen = Transformer.ToScreen(Polygons[i][j]);
                    screen.X = screen.X + movepoint.X;
                    screen.Y = screen.Y + movepoint.Y;
                    PointF temp = Transformer.FromScreen(screen);
                    //PointF temp = new PointF((points[i].X + movepoint.X), (points[i].Y + movepoint.Y));
                    Polygons[i].RemoveAt(j);
                    Polygons[i].Insert(j, temp);
                }
            }
            if (island != null)
            {
                for (int i = 0; i < island.Count(); i++)
                {
                    Point screen = new Point();
                    screen = Transformer.ToScreen(island[i]);
                    screen.X = screen.X + movepoint.X;
                    screen.Y = screen.Y + movepoint.Y;
                    PointF temp = Transformer.FromScreen(screen);
                    island.RemoveAt(i);
                    island.Insert(i,temp);
                }
            }
        }
        public void Dragsimplepolygon(Point movepoint)
        {
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                if (Controller.selectsimple[i]==0&&island != null)
                {
                    for (int k = 0; k< island.Count();k++)
                    {
                        Point screen = new Point();
                        screen = Transformer.ToScreen(island[k]);
                        screen.X = screen.X + movepoint.X;
                        screen.Y = screen.Y + movepoint.Y;
                        PointF temp = Transformer.FromScreen(screen);
                        island.RemoveAt(k);
                        island.Insert(k, temp);
                    }
                }
                for (int j = 0; j < Polygons[Controller.selectsimple[i]].Count(); j++)
                {
                    Point screen = new Point();
                    screen = Transformer.ToScreen(Polygons[Controller.selectsimple[i]][j]);
                    screen.X = screen.X + movepoint.X;
                    screen.Y = screen.Y + movepoint.Y;
                    PointF temp = Transformer.FromScreen(screen);
                    //PointF temp = new PointF((points[i].X + movepoint.X), (points[i].Y + movepoint.Y));
                    Polygons[Controller.selectsimple[i]].RemoveAt(j);
                    Polygons[Controller.selectsimple[i]].Insert(j, temp);
                }
            }
        }
        public void DragNode(Point movepoint)
        {
            Point screen = new Point();
            screen = Transformer.ToScreen(Polygons[Controller.selectnodeindex.X][Controller.selectnodeindex.Y]);
            screen.X = screen.X + movepoint.X;
            screen.Y = screen.Y + movepoint.Y;
            PointF temp = Transformer.FromScreen(screen);
            //PointF temp = new PointF((points[i].X + movepoint.X), (points[i].Y + movepoint.Y));
            Polygons[Controller.selectnodeindex.X].RemoveAt(Controller.selectnodeindex.Y);
            Polygons[Controller.selectnodeindex.X].Insert(Controller.selectnodeindex.Y, temp);
        }
        public Bitmap drawsimplepolygon(Bitmap currentbmp)
        {
            Graphics a = Graphics.FromImage(currentbmp);
            //CPolygonLegend cpl = new CPolygonLegend();
            Point screenp1 = new Point();
            Point screenp2 = new Point();
            Pen ppen = new Pen(Color.Transparent);
            ppen.Color = Color.Red;
            if (polygonpen.DashStyle != System.Drawing.Drawing2D.DashStyle.Solid)
            {
                float[] dashValues = { 3, 3 };
                ppen.DashPattern = dashValues;
            }
            ppen.Width = polygonpen.Width + 2;
            for (int j = 0; j <Controller.selectsimple.Count(); j++)
            {
                if (Controller.selectsimple[j] == 0 && island != null)
                {
                    a=enableisland(a);
                    continue;
                }
                Point[] polygonpoints = new Point[Polygons[Controller.selectsimple[j]].Count()];
                int m;
                for (m = 0; m + 1 < Polygons[Controller.selectsimple[j]].Count(); m++)
                {
                    screenp1 = Transformer.ToScreen(Polygons[Controller.selectsimple[j]][m]);
                    screenp2 = Transformer.ToScreen(Polygons[Controller.selectsimple[j]][m + 1]);
                    //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                    //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                    polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                }
                screenp1 = Transformer.ToScreen(Polygons[Controller.selectsimple[j]][m]);
                //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                screenp2 = Transformer.ToScreen(Polygons[Controller.selectsimple[j]][0]);
                //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                a.FillPolygon(polygonbrush, polygonpoints);
            }
            for (int n = 0; n < Controller.selectsimple.Count(); n++)
            {
                int m;
                if (Controller.selectsimple [n]== 0 && island != null)
                {
                    for (m = 0; m + 1 < island.Count(); m++)
                    {
                        screenp1 = Transformer.ToScreen(island[m]);
                        screenp2 = Transformer.ToScreen(island[m + 1]);
                        a.DrawLine(ppen, screenp1, screenp2);
                    }
                    screenp1 = Transformer.ToScreen(island[m]);
                    screenp2 = Transformer.ToScreen(island[0]);
                    a.DrawLine(ppen, screenp1, screenp2);
                }
                for (m = 0; m + 1 <Polygons[Controller.selectsimple[n]].Count(); m++)
                {
                    screenp1 = Transformer.ToScreen(Polygons[Controller.selectsimple[n]][m]);
                    screenp2 = Transformer.ToScreen(Polygons[Controller.selectsimple[n]][m + 1]);
                    a.DrawLine(ppen, screenp1, screenp2);
                }
                screenp1 = Transformer.ToScreen(Polygons[Controller.selectsimple[n]][m]);
                screenp2 = Transformer.ToScreen(Polygons[Controller.selectsimple[n]][0]);
                a.DrawLine(ppen, screenp1, screenp2);
            }
            return currentbmp;
        }
        public void selectsimplepolygon(PointF e)
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = Math.Max(((CPolygonLegend)Controller.MapLegends[LegendID]).border_weight * trans / 2, 10 * trans);
            if (MinBR.Lx - Buffer < e.X &&MinBR.Ly - Buffer < e.Y &&
                MinBR.Rx + Buffer > e.X && MinBR.Ry + Buffer > e.Y)
            {
                for (int j = 0; j <Polygons.Count(); j++)
                {
                    List<PointF> RecBuffer = new List<PointF>();
                    if (j == 0 && island != null)
                    {
                        for (int k = 0; k < island.Count(); k++)
                        {
                            RecBuffer.Add(island[k]);
                        }
                        if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                            continue;
                    }
                    for (int k = 0; k < Polygons[j].Count(); k++)
                        RecBuffer.Add(Polygons[j][k]);
                    //RecBuffer.Add(Polygons[j][0]);
                    if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                    {
                        for (int m = 0; m < Controller.selectsimple.Count(); m++)
                        {
                            if (j == Controller.selectsimple[m])
                            {
                                //Polygon_Set[i].isselected = false;
                                Controller.selectsimple.RemoveAt(m);
                                return;
                            }
                        }
                        //Controller.SelectedObjectType = "Polygon";
                        Controller.selectsimple.Add(j);
                        //Polygon_Set[i].isselected = true;
                    }
                }
            }
            if (Controller.selectsimple.Count == 1)
                MainForm.pMainWin.Split.Enabled = true;
            else
                MainForm.pMainWin.Split.Enabled = false;
        }
        public void selectnodeorline(PointF e)
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = 10 * trans;
            if (MinBR.Lx - Buffer < e.X &&
                MinBR.Ly - Buffer < e.Y &&
               MinBR.Rx + Buffer > e.X &&
                MinBR.Ry + Buffer > e.Y)
            {
                for (int i = 0; i <Polygons.Count(); i++)
                {
                    int j = 0;
                    for (j = 0; j < Polygons[i].Count(); j++)
                        if (Math.Abs(Polygons[i][j].X - e.X) < Buffer
                            && Math.Abs(Polygons[i][j].Y - e.Y) < Buffer)
                        {
                            if (i == Controller.selectnodeindex.X && j == Controller.selectnodeindex.Y)
                            {
                                Controller.selectnodeindex = new Point(-1, -1);
                                return;
                            }
                            Controller.selectnodeindex = new Point(i, j);
                            if (Controller.snapsameedge != null)
                            {
                                if (Controller.snapsameedge[1] == -1)
                                {
                                    if(Controller.snapsameedge.Count()==2)
                                        Controller.snapsameedge.Add(i);
                                    Controller.snapsameedge.Add(j);
                                }
                            }
                            Controller.selectlineindex = new Point(-1, -1);
                            return;
                        }
                    float PI = (float)Math.PI;
                    Buffer = Math.Max(((CPolygonLegend)Controller.MapLegends[LegendID]).border_weight* trans / 2, 10 * trans);
                    List<PointF> RecBuffer;
                    PointF P1, P2;
                    float slope;
                    for (j = 0; j + 1 < Polygons[i].Count(); j++)
                    {
                        RecBuffer = new List<PointF>();
                        P1 = Polygons[i][j];
                        P2 = Polygons[i][j + 1];
                        slope = (float)Math.Atan((P1.Y - P2.Y) / (P1.X - P2.X));
                        RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                        RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope + 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope + 0.75 * PI)));
                        RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope + 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope + 0.25 * PI)));
                        RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope - 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope - 0.25 * PI)));
                        //RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                        if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                        {
                            if (i == Controller.selectlineindex.X && j == Controller.selectlineindex.Y)
                            {
                                Controller.selectlineindex = new Point(-1, -1);
                                return;
                            }
                            Controller.selectlineindex = new Point(i, j);
                            Controller.selectnodeindex = new Point(-1, -1);
                            if (Controller.snapsameedge != null)
                            {
                                if (Controller.snapsameedge[1] == -2)
                                {
                                    if (Controller.snapsameedge.Count() == 2)
                                        Controller.snapsameedge.Add(i);
                                    Controller.snapsameedge.Add(j);
                                }
                            }
                            return;
                        }
                    }
                    RecBuffer = new List<PointF>();
                    P1 = Polygons[i][j];
                    P2 = Polygons[i][0];
                    slope = (float)Math.Atan((P1.Y - P2.Y) / (P1.X - P2.X));
                    RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                    RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope + 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope + 0.75 * PI)));
                    RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope + 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope + 0.25 * PI)));
                    RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope - 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope - 0.25 * PI)));
                    //RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                    if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                    {
                        if (i == Controller.selectlineindex.X && j == Controller.selectlineindex.Y)
                        {
                            Controller.selectlineindex = new Point(-1, -1);
                            return;
                        }
                        if (Controller.snapsameedge != null)
                        {
                            if (Controller.snapsameedge[1] == -2)
                            {
                                if (Controller.snapsameedge.Count() == 2)
                                    Controller.snapsameedge.Add(i);
                                Controller.snapsameedge.Add(j);
                            }
                        }
                        Controller.selectlineindex = new Point(i, j);
                        Controller.selectnodeindex = new Point(-1, -1);
                        return;
                    }
                }
            }
        }
        public bool Split(List<PointF> s)
        {
            PointF p1=new PointF (-1,-1);
            PointF p2 = new PointF(-1, -1); 
            PointF p3 = new PointF(-1, -1); 
            PointF p4 = new PointF(-1, -1);
            int sindex1=0; int sindex2=0;
            
            int interindex1=0;
            int interindex2=0;
            PointF intertemp = new PointF();
            List<PointF> Temp=new List<PointF> ();
            if (Controller.selectsimple == null || Controller.selectsimple.Count == 0)
            {
                if (s.Count == 2)
                {
                    p1.X = s[0].X; p1.Y = s[0].Y;
                    p2.X = s[1].X; p2.Y = s[1].Y;
                    p3.X = s[0].X; p3.Y = s[0].Y;
                    p4.X = s[1].X; p4.Y = s[1].Y;
                }
                else
                {
                    for (int i = 0; i + 1 < s.Count; i++)
                    {
                        if ((!Controller.IfPointInPolygon(Polygons[0], s[i])) && Controller.IfPointInPolygon(Polygons[0], s[i + 1]))
                        {
                            p1.X = s[i].X; p1.Y = s[i].Y;
                            p2.X = s[i + 1].X; p2.Y = s[i + 1].Y;
                            sindex1 = i + 1;
                            continue;
                        }
                        if (Controller.IfPointInPolygon(Polygons[0], s[i]) && (!Controller.IfPointInPolygon(Polygons[0], s[i + 1])))
                        {
                            p3.X = s[i].X; p3.Y = s[i].Y;
                            p4.X = s[i + 1].X; p4.Y = s[i + 1].Y;
                            sindex2 = i;
                            break;
                        }
                    }
                }
                if (p3.Y == -1)
                    return false;
                Polygons[0].Add(Polygons[0][0]);
                for (int i = 0; i + 1 < Polygons[0].Count; i++)
                {
                    intertemp = Controller.SegmentIntersect(Polygons[0][i], Polygons[0][i + 1], p1, p2);
                    if (intertemp.X == -1 && intertemp.Y == -1)
                        continue;
                    else
                    {
                        inter1 = new PointF();
                        inter1.X = intertemp.X;
                        inter1.Y = intertemp.Y;
                        Polygons[0].Insert(i + 1, inter1);
                        interindex1 = i + 1;
                        break;
                    }
                }
                for (int i = 0; i + 1 < Polygons[0].Count; i++)
                {
                    intertemp = Controller.SegmentIntersect(Polygons[0][i], Polygons[0][i + 1], p3, p4);
                    if (intertemp.X == -1 && intertemp.Y == -1)
                        continue;
                    else if(inter1.X!=intertemp.X)
                    {
                        inter2 = new PointF();
                        inter2.X = intertemp.X;
                        inter2.Y = intertemp.Y;
                        Polygons[0].Insert(i + 1, inter2);
                        interindex2 = i + 1;
                        break;
                    }
                }
                if (inter1 == null || inter2 == null)
                    return false;
                List<PointF> Templist = new List<PointF>();
                Polygons[0].RemoveAt(Polygons[0].Count - 1);
                if (interindex1 < interindex2)
                {
                    int k = Polygons[0].Count;
                    Templist.Add(inter2);
                    for (int i = interindex2 + 1; i < k; i++)
                    {
                        Templist.Add(Polygons[0][interindex2 + 1]);
                        Polygons[0].RemoveAt(interindex2 + 1);
                    }
                    for (int i = 0; i < interindex1; i++)
                    {
                        Templist.Add(Polygons[0][0]);
                        Polygons[0].RemoveAt(0);
                    }
                    Templist.Add(inter1);
                }
                else if (interindex1 > interindex2)
                {
                    interindex1++;
                    Templist.Add(inter1);
                    int k = Polygons[0].Count;
                    for (int i = interindex1 + 1; i < k; i++)
                    {
                        Templist.Add(Polygons[0][interindex1 + 1]);
                        Polygons[0].RemoveAt(interindex1 + 1);
                    }
                    for (int i = 0; i < interindex2; i++)
                    {
                        Templist.Add(Polygons[0][0]);
                        Polygons[0].RemoveAt(0);
                    }
                    Templist.Add(inter2);
                }
                else
                    return false;
                if (sindex1 == 0)
                {
                    Polygons.Add(Templist);
                    return true;
                }
                else if (sindex1 == sindex2)
                {
                    Templist.Add(s[sindex2]);
                    Polygons[0].Add(s[sindex2]);
                }
                else
                {
                    if (interindex1 < interindex2)
                    {
                        int k = Polygons[0].Count;
                        for (int i = sindex1; i <= sindex2; i += 1)
                        {
                            Templist.Add(s[i]);
                            Polygons[0].Insert(k, s[i]);
                        }
                    }
                    else
                    {
                        int k = Templist.Count;
                        for (int i = sindex1; i <= sindex2; i += 1)
                        {
                            Templist.Insert(k, s[i]);
                            Polygons[0].Add(s[i]);
                        }
                    }
                }
                Polygons.Add(Templist);

                return true;
            }
            else
            {
                if (s.Count == 2)
                {
                    p1.X = s[0].X; p1.Y = s[0].Y;
                    p2.X = s[1].X; p2.Y = s[1].Y;
                    p3.X = s[0].X; p3.Y = s[0].Y;
                    p4.X = s[1].X; p4.Y = s[1].Y;
                }
                else
                {
                    for (int i = 0; i + 1 < s.Count; i++)
                    {
                        if ((!Controller.IfPointInPolygon(Polygons[Controller.selectsimple[0]], s[i])) && Controller.IfPointInPolygon(Polygons[Controller.selectsimple[0]], s[i + 1]))
                        {
                            p1.X = s[i].X; p1.Y = s[i].Y;
                            p2.X = s[i + 1].X; p2.Y = s[i + 1].Y;
                            sindex1 = i + 1;
                            continue;
                        }
                        if (Controller.IfPointInPolygon(Polygons[Controller.selectsimple[0]], s[i]) && (!Controller.IfPointInPolygon(Polygons[Controller.selectsimple[0]], s[i + 1])))
                        {
                            p3.X = s[i].X; p3.Y = s[i].Y;
                            p4.X = s[i + 1].X; p4.Y = s[i + 1].Y;
                            sindex2 = i;
                            break;
                        }
                    }
                }
                if (p3.Y == -1)
                    return false;
                Polygons[Controller.selectsimple[0]].Add(Polygons[Controller.selectsimple[0]][0]);
                for (int i = 0; i + 1 < Polygons[Controller.selectsimple[0]].Count; i++)
                {
                    intertemp = Controller.SegmentIntersect(Polygons[Controller.selectsimple[0]][i], Polygons[Controller.selectsimple[0]][i + 1], p1, p2);
                    if (intertemp.X == -1 && intertemp.Y == -1)
                        continue;
                    else
                    {
                        inter1 = new PointF();
                        inter1.X = intertemp.X;
                        inter1.Y = intertemp.Y;
                        Polygons[Controller.selectsimple[0]].Insert(i + 1, inter1);
                        interindex1 = i + 1;
                        break;
                    }
                }
                for (int i = 0; i + 1 < Polygons[Controller.selectsimple[0]].Count; i++)
                {
                    intertemp = Controller.SegmentIntersect(Polygons[Controller.selectsimple[0]][i], Polygons[Controller.selectsimple[0]][i + 1], p3, p4);
                    if (intertemp.X == -1 && intertemp.Y == -1)
                        continue;
                    else if (inter1.X != intertemp.X)
                    {
                        inter2 = new PointF();
                        inter2.X = intertemp.X;
                        inter2.Y = intertemp.Y;
                        Polygons[Controller.selectsimple[0]].Insert(i + 1, inter2);
                        interindex2 = i + 1;
                        break;
                    }
                }
                if (inter1 == null || inter2 == null)
                    return false;
                List<PointF> Templist = new List<PointF>();
                Polygons[Controller.selectsimple[0]].RemoveAt(Polygons[Controller.selectsimple[0]].Count - 1);
                if (interindex1 < interindex2)
                {
                    int k = Polygons[Controller.selectsimple[0]].Count;
                    Templist.Add(inter2);
                    for (int i = interindex2 + 1; i < k; i++)
                    {
                        Templist.Add(Polygons[Controller.selectsimple[0]][interindex2 + 1]);
                        Polygons[Controller.selectsimple[0]].RemoveAt(interindex2 + 1);
                    }
                    for (int i = 0; i < interindex1; i++)
                    {
                        Templist.Add(Polygons[Controller.selectsimple[0]][0]);
                        Polygons[Controller.selectsimple[0]].RemoveAt(0);
                    }
                    Templist.Add(inter1);
                }
                else if (interindex1 > interindex2)
                {
                    interindex1++;
                    Templist.Add(inter1);
                    int k = Polygons[Controller.selectsimple[0]].Count;
                    for (int i = interindex1 + 1; i < k; i++)
                    {
                        Templist.Add(Polygons[Controller.selectsimple[0]][interindex1 + 1]);
                        Polygons[Controller.selectsimple[0]].RemoveAt(interindex1 + 1);
                    }
                    for (int i = 0; i < interindex2; i++)
                    {
                        Templist.Add(Polygons[Controller.selectsimple[0]][0]);
                        Polygons[Controller.selectsimple[0]].RemoveAt(0);
                    }
                    Templist.Add(inter2);
                }
                else
                    return false;
                if (sindex1 == 0)
                {
                    Polygons.Add(Templist);
                    return true;
                }
                else if (sindex1 == sindex2)
                {
                    Templist.Add(s[sindex2]);
                    Polygons[Controller.selectsimple[0]].Add(s[sindex2]);
                }
                else
                {
                    if (interindex1 < interindex2)
                    {
                        int k = Polygons[Controller.selectsimple[0]].Count;
                        for (int i = sindex1; i <= sindex2; i += 1)
                        {
                            Templist.Add(s[i]);
                            Polygons[Controller.selectsimple[0]].Insert(k, s[i]);
                        }
                    }
                    else
                    {
                        int k = Templist.Count;
                        for (int i = sindex1; i <= sindex2; i += 1)
                        {
                            Templist.Insert(k, s[i]);
                            Polygons[Controller.selectsimple[0]].Add(s[i]);
                        }
                    }
                }
                Polygons.Add(Templist);

                return true;
            }
            return false;
        }

        public void Calculate_Perimeter()
        {
            Perimeter = new double[Polygons.Count + 1];
            for (int i = 0; i < Polygons.Count; i++)
            {
                int j;
                for (j = 0; j < Polygons[i].Count - 1; j++)
                {
                    Perimeter[i] += Math.Sqrt((Polygons[i][j].X - Polygons[i][j + 1].X) * (Polygons[i][j].X - Polygons[i][j + 1].X) + (Polygons[i][j].Y - Polygons[i][j + 1].Y) * (Polygons[i][j].Y - Polygons[i][j + 1].Y));
                }
                Perimeter[i] += Math.Sqrt((Polygons[i][j].X - Polygons[i][0].X) * (Polygons[i][j].X - Polygons[i][0].X) + (Polygons[i][j].Y - Polygons[i][0].Y) * (Polygons[i][j].Y - Polygons[i][0].Y));
                Perimeter[Polygons.Count] += Perimeter[i];
            }
        }

        public void Calculate_Area()
        {
            Area =new double[Polygons.Count+1];
            for (int i = 0; i < Polygons.Count; i++)
            {
                for (int j = 0; j < Polygons[i].Count - 2; j++)
                {
                    Area[i]+=(Polygons[i][j].X*Polygons[i][j+1].Y-Polygons[i][j+1].X*Polygons[i][j].Y);
                }
                Area[i] += (Polygons[i][Polygons[i].Count - 1].X * Polygons[i][0].Y - Polygons[i][0].X * Polygons[i][Polygons[i].Count - 1].Y);
                Area[i] = Math.Abs(Area[i])/2;
                Area[Polygons.Count] += Area[i];
            }
        }
        //以下为扫描线算法
        public struct Edge                                                  //边的数据结构
        {
            public int Ymax;                                                //边的y坐标的最大值（下端点，由于坐标系y轴向下）
            public int Ymin;                                                //边的y坐标的最小值（上端点）
            public double X;                                                //上端点x坐标
            public double Deltax;                                           //边的斜率的倒数
        }

        public struct EdgeClass                                             //边的活化链表AEL数据结构
        {
            public List<Edge> Edges;
        }

        public struct EdgeTable                                             //边的分类表的数据结构
        {
            public List<EdgeClass> Edgeclasses;
        }

        EdgeClass AEL = new EdgeClass();                                    //边的活化链表AEL
        EdgeTable ET = new EdgeTable();                                     //边的分类表ET                            
        EdgeClass Polyedges = new EdgeClass();                              //多边形的边的信息
        public List<Point> tempPolygon = new List<Point>();
        public int YMAX = 0;                                                //记录所有边纵坐标的最大值
        public int count = 0;

        //初始化temppolygon
        private void enabletemp()
        {
            Point screen = new Point();
            for (int i = 0; i < Polygons[0].Count(); i++)
            {
                screen = Transformer.ToScreen(Polygons[0][i]);
                tempPolygon.Add(screen);
            }
            screen = Transformer.ToScreen(Polygons[0][0]);
            tempPolygon.Add(screen);
            count=tempPolygon.Count();
            for(int i=0;i<island.Count();i++)
            {
                screen = Transformer.ToScreen(island[i]);
                tempPolygon.Add(screen);
            }
            screen = Transformer.ToScreen(island[0]);
            tempPolygon.Add(screen);
        }
        //生成边的分类表ET结构
        private void Form_structure()
        {
            Polyedges.Edges = new List<Edge>();
            Edge e = new Edge();
            for (int i = 0; i < count - 1; i++)
            {
                if (YMAX < tempPolygon[i].Y)                                           //在此过程中同时求出各多边形的Y坐标的最大值
                {
                    YMAX = tempPolygon[i].Y;
                }
                if (tempPolygon[i].Y != tempPolygon[i + 1].Y)                           //水平边不写入结构中
                {
                    e.Ymin = Math.Min(tempPolygon[i].Y, tempPolygon[i + 1].Y);
                    e.Ymax = Math.Max(tempPolygon[i].Y, tempPolygon[i + 1].Y);
                    if (e.Ymin == tempPolygon[i].Y)
                    {
                        e.X = tempPolygon[i].X;
                    }
                    else
                    {
                        e.X = tempPolygon[i + 1].X;
                    }
                    e.Deltax = Convert.ToDouble((tempPolygon[i].X - tempPolygon[i + 1].X)) / Convert.ToDouble((tempPolygon[i].Y - tempPolygon[i + 1].Y));
                    Polyedges.Edges.Add(e);
                }
            }
            for (int i = count; i < tempPolygon.Count - 1; i++)                         //将多边形点的信息转化成边的信息，存入Polyedges中
            {
                if (YMAX < tempPolygon[i].Y)                                           //在此过程中同时求出各多边形的Y坐标的最大值
                {
                    YMAX = tempPolygon[i].Y;
                }
                if (tempPolygon[i].Y != tempPolygon[i + 1].Y)                           //水平边不写入结构中
                {
                    e.Ymin = Math.Min(tempPolygon[i].Y, tempPolygon[i + 1].Y);
                    e.Ymax = Math.Max(tempPolygon[i].Y, tempPolygon[i + 1].Y);
                    if (e.Ymin == tempPolygon[i].Y)
                    {
                        e.X = tempPolygon[i].X;
                    }
                    else
                    {
                        e.X = tempPolygon[i + 1].X;
                    }
                    e.Deltax = Convert.ToDouble((tempPolygon[i].X - tempPolygon[i + 1].X)) / Convert.ToDouble((tempPolygon[i].Y - tempPolygon[i + 1].Y));
                    Polyedges.Edges.Add(e);
                }
            }

            ET.Edgeclasses = new List<EdgeClass>();
            for (int i = 0; i < YMAX; i++)                                            //初始化边的分类表ET
            {                                                                        //建立0~YMAX-1类
                Edge empty = new Edge();
                empty.X = -1;
                EdgeClass Em = new EdgeClass();
                Em.Edges = new List<Edge>();
                Em.Edges.Add(empty);
                for (int j = 0; j < Polyedges.Edges.Count; j++)                      //找到Polyedges.Edges[j].Ymin == i的边加入相应的ET.Edgeclasses中
                    if (Polyedges.Edges[j].Ymin == i)                                //若是没有边，用空的占位置
                        Em.Edges.Add(Polyedges.Edges[j]);
                ET.Edgeclasses.Add(Em);
            }
            for (int i = 0; i < YMAX; i++)                                                     //对每一类中的边进行排序
            {
                if (ET.Edgeclasses[i].Edges.Count > 1)                                   //只有占位边的不排序
                {
                    Sort_X(ET.Edgeclasses[i], 1);
                }
            }
        }

        //进行扫描转换
        private Graphics Change(Graphics graph)
        {
            AEL.Edges = new List<Edge>();
            List<Point> InterPoint = new List<Point>();
            EdgeClass nonExtreme = new EdgeClass();
            nonExtreme.Edges = new List<Edge>();
            for (int i = 0; i < YMAX; i++)                                                                   // i++实现了y=y+1
            {
                if (ET.Edgeclasses[i].Edges.Count > 1)                                                        //对非“空”的边的类排序
                {
                    for (int j = 1; j < ET.Edgeclasses[i].Edges.Count; j++)
                    {
                        Point pt = new Point();
                        pt.X = (int)ET.Edgeclasses[i].Edges[j].X;
                        pt.Y = i;
                        if (JudgePoint(pt) == true)                                                            //判断端点是否为极值点
                        {
                            AEL.Edges.Add(ET.Edgeclasses[i].Edges[j]);                                         //极值点加入IntrePoint中，表示交点
                        }
                        else
                        {
                            nonExtreme.Edges.Add(ET.Edgeclasses[i].Edges[j]);                                 //非极值点暂时存入nonExtreme中
                        }
                    }
                }

                if (AEL.Edges.Count != 0)                                                                   //AEL非空时进行操作
                {
                    Sort_X(AEL, 0);                                                                          //对AEL进行排序
                    for (int j = 0; j < AEL.Edges.Count; j++)                                               //获得当前扫描线的交点
                    {
                        Point pt = new Point();
                        pt.X = (int)AEL.Edges[j].X;
                        pt.Y = i;
                        InterPoint.Add(pt);
                    }
                    //graph = Graphics.FromImage(currentbmp);
                    for (int j = 0; j < InterPoint.Count; )                                                 //各交点配对绘制像素
                    {
                        try
                        {
                            Pen fillpen = new Pen(polygonbrush.Color);
                            graph.DrawLine(fillpen, InterPoint[j], InterPoint[j + 1]);
                            j = j + 2;
                        }
                        catch
                        {
                            MessageBox.Show("画岛出现错误，请检查！");
                            island.Clear();
                            InterPoint.Clear();
                            ((CPolygonLayer)Controller.CurrentLayer).drawpolygon();
                            MainForm.pMainWin.Island.PerformClick();
                        }
                    }

                    for (int j = 0; j < nonExtreme.Edges.Count; j++)                                        //此时将nonExtreme中存储的非极值点加入
                    {
                        AEL.Edges.Add(nonExtreme.Edges[j]);
                    }
                    nonExtreme.Edges.Clear();                                                               //清空nonExtreme
                    int totalnum = AEL.Edges.Count;
                    for (int j = 0; j < totalnum; j++)                                                      //去掉走到i=Ymax的边
                    {
                        if (i == AEL.Edges[j].Ymax)
                        {
                            AEL.Edges.Remove(AEL.Edges[j]);
                            totalnum = totalnum - 1;
                            j = j - 1;
                        }
                        else                                                                               //X=X+Deltax
                        {
                            Edge temp = new Edge();
                            temp.X = AEL.Edges[j].X + AEL.Edges[j].Deltax;
                            temp.Ymax = AEL.Edges[j].Ymax;
                            temp.Ymin = AEL.Edges[j].Ymin;
                            temp.Deltax = AEL.Edges[j].Deltax;
                            AEL.Edges.Add(temp);
                        }
                    }
                    totalnum = AEL.Edges.Count;
                    for (int j = 0; j < totalnum / 2; j++)                                                  //删除之前结点
                    {
                        AEL.Edges.Remove(AEL.Edges[0]);
                    }
                    InterPoint.Clear();
                }
            }
            for (int i = 0; i < count - 1; i++)
            {
                graph.DrawLine(polygonpen, tempPolygon[i], tempPolygon[i + 1]);
            }
            for (int i = count; i < tempPolygon.Count - 1; i++)                                                     //重新绘制边界
            {
                graph.DrawLine(polygonpen, tempPolygon[i], tempPolygon[i + 1]);
            }
            //pictureBox1.Image = newBitmap;

            AEL.Edges.Clear();                                                                           //清空各数据结构
            ET.Edgeclasses.Clear();
            InterPoint.Clear();
            nonExtreme.Edges.Clear();
            tempPolygon.Clear();
            return graph;
        }

        //判断当前端点是否为极值点
        public bool JudgePoint(Point pt)
        {
            bool judge = false;
            int i;
            for (i = 0; i < tempPolygon.Count; i++)                                                              //在tempPolygon中找到该点
            {
                if (i != count - 1 && i != tempPolygon.Count - 1)
                {
                    if (pt.X == tempPolygon[i].X && pt.Y == tempPolygon[i].Y)
                        break;
                }
            }

            if (i == 0)                                                                                      //第一个点的情况                                                              
            {
                if ((tempPolygon[count - 2].Y - tempPolygon[0].Y) * (tempPolygon[1].Y - tempPolygon[0].Y) >= 0)
                    judge = true;                                                                            //是极值点
            }
            else if (i > 0 && i < tempPolygon.Count - 1)                                                         //其余点
            {
                if ((tempPolygon[i - 1].Y - tempPolygon[i].Y) * (tempPolygon[i + 1].Y - tempPolygon[i].Y) >= 0)
                    judge = true;
            }
            if (i == tempPolygon.Count)                                                                             //非多边形端点
                judge = false;

            return judge;
        }

        //利用选择法对EdgeClass结构按x递增的顺序排序
        void Sort_X(EdgeClass Previou, int start)
        {
            Edge temp = new Edge();
            for (int i = start; i < Previou.Edges.Count - 1; i++)
            {
                int k = i;
                for (int j = i + 1; j < Previou.Edges.Count; j++)
                {
                    if (Previou.Edges[j].X < Previou.Edges[k].X)
                        k = j;
                    else if (Previou.Edges[j].X == Previou.Edges[k].X && Previou.Edges[j].Deltax < Previou.Edges[k].Deltax)//x相同时，比较Deltax
                        k = j;
                }
                if (k != i)
                {
                    temp = Previou.Edges[i];
                    Previou.Edges[i] = Previou.Edges[k];
                    Previou.Edges[k] = temp;
                }
            }
        }

        public Graphics enableisland(Graphics graph)
        {
            enabletemp();
            Form_structure();
            Change(graph);
            return graph;
        }
    }
}
